# coding=utf-8
from string import ascii_lowercase
from random import choice, random

target  = list("welcome to http://www.cnhup.com")
charset = ascii_lowercase + ' .:/'
parent  = [choice(charset) for _ in range(len(target))]
minmutaterate  = .09
C = range(500)

perfectfitness = len(target)
def fitness(trial):
    return sum(t==h for t,h in zip(trial, target))

def mutaterate(parent):
    return 1.0-(1.0*(perfectfitness - fitness(parent)) / perfectfitness * (1.0 - minmutaterate))

def mutate(parent, rate):
    return [(ch if random() <= rate else choice(charset)) for ch in parent]

def log(iterations,rate,parent):
    # 控制台输出
    print ("#%-4i, rate: %4.3f, fitness: %4.1f%%, '%s'" %
           (iterations, rate, fitness(parent)*100./perfectfitness, ''.join(parent)))

iterations = 0
while parent != target:         # 当不符合要求的时候,继续循环
    rate =  mutaterate(parent)  # 计算当前匹配结果的适应度
    iterations += 1             # 繁殖代数加一
    if iterations % 10 == 0:    # 每迭代10次输出一下当前结果
        log(iterations, rate, parent)
    copies = [mutate(parent, rate) for _ in C ]  + [parent]
    parent = max(copies, key=fitness)

print ()
log(iterations,rate,parent)